﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MahApps.Metro.Controls">

    <ControlTemplate TargetType="{x:Type local:SplitButton}"
                     x:Key="SplitButtonHorizontal">
        <Border x:Name="PART_Border"
                UseLayoutRounding="True"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                BorderThickness="{TemplateBinding BorderThickness}"
                BorderBrush="{TemplateBinding BorderBrush}">
            <Grid x:Name="PART_Container"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0"
                        x:Name="PART_Button"
                        Style="{TemplateBinding ButtonStyle}"
                        FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="Stretch"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Background="{TemplateBinding Background}"
                        Foreground="{TemplateBinding Foreground}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Command="{TemplateBinding Command}"
                        CommandParameter="{TemplateBinding CommandParameter}"
                        CommandTarget="{TemplateBinding CommandTarget}">
                    <StackPanel Orientation="Horizontal"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                        <ContentPresenter UseLayoutRounding="False"
                                          RecognizesAccessKey="True"
                                          Focusable="False"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Content="{TemplateBinding Icon}"
                                          ContentTemplate="{TemplateBinding IconTemplate}" />
                        <ContentControl x:Name="PART_ButtonContent"
                                        UseLayoutRounding="False"
                                        Focusable="False"
                                        Margin="2"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        ContentStringFormat="{TemplateBinding ItemStringFormat}"
                                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                        Content="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                        ContentTemplate="{TemplateBinding ItemTemplate}" />
                    </StackPanel>
                </Button>
                <Button x:Name="PART_Expander"
                        Grid.Column="1"
                        Style="{TemplateBinding ButtonArrowStyle}"
                        FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Background="{TemplateBinding Background}"
                        Foreground="{TemplateBinding Foreground}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Width="20">
                    <Rectangle x:Name="PART_Rectangle"
                               UseLayoutRounding="False"
                               Height="6"
                               Width="10"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Fill="{TemplateBinding ArrowBrush}">
                        <Rectangle.OpacityMask>
                            <VisualBrush Stretch="Fill">
                                <VisualBrush.Visual>
                                    <Canvas Width="18"
                                            Height="18"
                                            Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0"
                                            UseLayoutRounding="False">
                                        <Path Width="18"
                                              Height="16"
                                              Canvas.Left="14.9063"
                                              Canvas.Top="15.75"
                                              Stretch="Fill"
                                              Fill="{DynamicResource BlackColorBrush}"
                                              Data="F1 M 23.9063,25L 25.9063,23L 32.9063,15.75L 32.9063,22.25L 23.9062,31.75L 14.9063,22.25L 14.9063,15.75L 21.9063,23L 23.9063,25 Z " />
                                    </Canvas>
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Rectangle.OpacityMask>
                    </Rectangle>
                </Button>
                <Popup x:Name="PART_Popup"
                       IsOpen="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                       PlacementTarget="{Binding ElementName=PART_Button}"
                       PopupAnimation="Fade"
                       MinWidth="{TemplateBinding ActualWidth}"
                       StaysOpen="True">
                    <ListBox x:Name="PART_ListBox"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             Style="{TemplateBinding ListBoxStyle}"
                             SelectionMode="Single"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                             ItemTemplate="{TemplateBinding ItemTemplate}"
                             ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                             ItemStringFormat="{TemplateBinding ItemStringFormat}"
                             ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                             ItemContainerStyleSelector="{TemplateBinding ItemContainerStyleSelector}"
                             ItemsSource="{TemplateBinding ItemsSource}" />
                </Popup>
            </Grid>
        </Border>
    </ControlTemplate>

    <ControlTemplate TargetType="{x:Type local:SplitButton}"
                     x:Key="SplitButtonVertical">
        <Border x:Name="PART_Border"
                UseLayoutRounding="True"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                BorderThickness="{TemplateBinding BorderThickness}"
                BorderBrush="{TemplateBinding BorderBrush}">
            <Grid x:Name="PART_Container"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Button Grid.Row="0"
                        x:Name="PART_Button"
                        Style="{TemplateBinding ButtonStyle}"
                        FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="Stretch"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Background="{TemplateBinding Background}"
                        Foreground="{TemplateBinding Foreground}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Command="{TemplateBinding Command}"
                        CommandParameter="{TemplateBinding CommandParameter}"
                        CommandTarget="{TemplateBinding CommandTarget}">
                    <StackPanel Orientation="Vertical"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                        <ContentPresenter UseLayoutRounding="False"
                                          RecognizesAccessKey="True"
                                          Focusable="False"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Content="{TemplateBinding Icon}"
                                          ContentTemplate="{TemplateBinding IconTemplate}" />
                        <ContentControl x:Name="PART_ButtonContent"
                                        UseLayoutRounding="False"
                                        Focusable="False"
                                        Margin="2"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        ContentStringFormat="{TemplateBinding ItemStringFormat}"
                                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                        Content="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                        ContentTemplate="{TemplateBinding ItemTemplate}" />
                    </StackPanel>
                </Button>
                <Button x:Name="PART_Expander"
                        Grid.Row="1"
                        Style="{TemplateBinding ButtonArrowStyle}"
                        FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Background="{TemplateBinding Background}"
                        Foreground="{TemplateBinding Foreground}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Height="20">
                    <Rectangle x:Name="PART_Rectangle"
                               UseLayoutRounding="False"
                               Height="6"
                               Width="10"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Fill="{TemplateBinding ArrowBrush}">
                        <Rectangle.OpacityMask>
                            <VisualBrush Stretch="Fill">
                                <VisualBrush.Visual>
                                    <Canvas Width="18"
                                            Height="18"
                                            Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0"
                                            UseLayoutRounding="False">
                                        <Path Width="18"
                                              Height="16"
                                              Canvas.Left="14.9063"
                                              Canvas.Top="15.75"
                                              Stretch="Fill"
                                              Fill="{DynamicResource BlackColorBrush}"
                                              Data="F1 M 23.9063,25L 25.9063,23L 32.9063,15.75L 32.9063,22.25L 23.9062,31.75L 14.9063,22.25L 14.9063,15.75L 21.9063,23L 23.9063,25 Z " />
                                    </Canvas>
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Rectangle.OpacityMask>
                    </Rectangle>
                </Button>
                <Popup x:Name="PART_Popup"
                       IsOpen="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                       PlacementTarget="{Binding ElementName=PART_Expander}"
                       PopupAnimation="Fade"
                       MinWidth="{TemplateBinding ActualWidth}"
                       StaysOpen="True">
                    <ListBox x:Name="PART_ListBox"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             Style="{TemplateBinding ListBoxStyle}"
                             SelectionMode="Single"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                             ItemTemplate="{TemplateBinding ItemTemplate}"
                             ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                             ItemStringFormat="{TemplateBinding ItemStringFormat}"
                             ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                             ItemContainerStyleSelector="{TemplateBinding ItemContainerStyleSelector}"
                             ItemsSource="{TemplateBinding ItemsSource}" />
                </Popup>
            </Grid>
        </Border>
    </ControlTemplate>

    <Style x:Key="ButtonSplitFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="1"
                               SnapsToDevicePixels="true"
                               Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                               StrokeThickness="1"
                               StrokeDashArray="1 2" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type local:SplitButton}">
        <Setter Property="ButtonStyle"
                Value="{DynamicResource ButtonSplitStyle}" />
        <Setter Property="ButtonArrowStyle"
                Value="{DynamicResource ButtonSplitArrowStyle}" />
        <Setter Property="ListBoxStyle"
                Value="{DynamicResource VirtualisedMetroListBox}" />
        <Setter Property="Background"
                Value="{DynamicResource WhiteColorBrush}" />
        <Setter Property="Foreground"
                Value="{DynamicResource BlackColorBrush}" />
        <Setter Property="ArrowBrush"
                Value="{DynamicResource BlackColorBrush}" />
        <Setter Property="MinHeight"
                Value="26" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource BlackColorBrush}" />
        <Setter Property="FontFamily"
                Value="{DynamicResource ContentFontFamily}" />
        <Setter Property="FontSize"
                Value="{DynamicResource ContentFontSize}" />
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="FocusVisualStyle"
                Value="{StaticResource ButtonSplitFocusVisual}" />
        <Setter Property="HorizontalContentAlignment"
                Value="Center" />
        <Setter Property="VerticalContentAlignment"
                Value="Center" />
        <Setter Property="Template"
                Value="{StaticResource SplitButtonHorizontal}" />
        <Style.Triggers>
            <Trigger Property="Orientation"
                     Value="Vertical">
                <Setter Property="Template"
                        Value="{StaticResource SplitButtonVertical}" />
            </Trigger>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter Property="ArrowBrush"
                        Value="{DynamicResource AccentColorBrush}" />
            </Trigger>
            <Trigger Property="IsFocused"
                     Value="True">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource AccentColorBrush}" />
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin"
                     Value="True">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource AccentColorBrush}" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>